public class DataUtil {
    public static String getStringValue(Object o ){
        if(o== null ||  o == 'null') return null;
        return String.valueOf(o).trim();
    }

    public static String getIdValue(Object o ){
        if(o== null ) return null;
        String s = getStringValue(o);
        if(String.isBlank(s) || s == 'null' )return null ;
        return Id.valueOf(s);
    }

    public static Decimal getDecimalValue(Object o ){
        String s = getStringValue(o);
        if(s == null || String.isBlank(s) ) return null;
        return Decimal.valueOf(s);
    }

    
     ///处理数据: 根据数据的类型返回指定数据类型的数据
    public static  Object dataHandler(object o, String type){
        if(type != null) type = type.toUpperCase();
        if(type == 'CURRENCY' || type == 'DOUBLE' ||type == 'PERCENT'){
        //小数
            return getDecimalValue(o);
        }else if(type == 'INTEGER' ){
        //整数

           return Integer.valueOf((String) o);
        }else if(type == 'DATE'){
        //日期
           return Date.valueOf((String) o);
        }else if(type == 'DATETIME'){
        //日期时间
           return DateTime.valueOf((String) o);
        }else if(type == 'TIME'){
        //时间
           String t = (String) o;
           if(t.length() == 0) return null;
           List<String>ts = t.split(':',-1);
           if(ts.size() == 1) return Time.newInstance(Integer.valueOf(ts[0]),0,0,0);
           return ts.size() > 2 ? Time.newInstance(Integer.valueOf(ts[0]),Integer.valueOf(ts[1]),Integer.valueOf(ts[2]),0):Time.newInstance(Integer.valueOf(ts[0]),Integer.valueOf(ts[1]),0,0);
        }else {
        //字符串
            return o;
        }
        
    }

    ///转换
    public static  Map<String,List<FieldBean>> getFieldsBean(Map<String,Set<String>>fieldsMap ,Map<String,SObjectBean> stypeMap  ){

        Map<String,List<FieldBean>> tmp = new Map<String,List<FieldBean>>();
       
        for(String objName: fieldsMap.keySet()){
            Map<String,FieldBean> fieldMap = getFieldsInfo(stypeMap.get(objName).stype  ,fieldsMap.get(objName));
            tmp.put(objName,fieldMap.values());
        }
        return tmp ;


    }
    //根据字段 和 对象 获取对象的值
    public static  Map<String,SObject>   getBaseInfo(Map<String,List<FieldBean>> fieldsMap,Map<String,SObjectBean> sbMap  ){

        Map<String,SObject>  sbValueMap = new Map<String,SObject>();
        for(String objname : fieldsMap.keySet()){

            List<String > tmp = new List<String>();
            for(FieldBean fb : fieldsMap.get(objname)){

                tmp.add(fb.name);
                if(fb.type == 'reference'){
                    tmp.add(fb.relation +'.Name');
                    if(fb.isRecordtype)tmp.add(fb.relation +'.RecordType.DeveloperName');
                    if(fb.reference.objname == 'Account')tmp.add(fb.relation +'.System__c');
                }

            }

            String sql = ' select  ' + String.join( tmp , ',') + ' from  '+ objname + '  where  Id =\''+sbMap.get(objname).idvalue+'\'' ;
            System.debug('DataUtil==>'+sql);
            SObject  obj1 = sbMap.get(objname).idvalue != null ?  Database.query(sql) : sbMap.get(objname).stype.newSObject();
            sbValueMap.put(objname,obj1);
        }

        return sbValueMap;
    }
    //设置根据对象设置字段的值
    public static  Map<String,List<FieldBean>> setFieldsValue(Map<String,List<FieldBean >>fieldsMap  ,Map<String,SObject> soMap  ){
        Map<String,List<FieldBean>> objMap   = new Map<String,List<FieldBean>>();
        
            for(String objName : fieldsMap.keySet()){
                List<FieldBean> fieldList = fieldsMap.get(objName);
                SObject so = soMap.get(objname);
                
                List<FieldBean>  fbL = new List<FieldBean>();
                for(FieldBean fb  : fieldList ){

                    if(fb.type == 'reference'){

                        Map<String,Object> vMap  = new Map<String,Object>();
                        vMap.put('Name', so.getSObject(fb.relation) == null ?  null : so.getSObject(fb.relation).get('Name') );
                        vMap.put('Id', so.getSObject(fb.relation) == null ?  null : so.getSObject(fb.relation).get('Id')  );
                        if(fb.isRecordtype)vMap.put('RecordType', so.getSObject(fb.relation) == null ?  null : so.getSObject(fb.relation).getSObject('RecordType') == null ? null  : so.getSObject(fb.relation).getSObject('RecordType').get('DeveloperName')  );
                        if(fb.reference.objname == 'Account')vMap.put('Third',so.getSObject(fb.relation) == null ?  null : so.getSObject(fb.relation).get('System__c')  );
                        fb.value = vMap;
                    }else{
                        fb.value = so.get(fb.name);
                    }
                    

                    fbL.add(fb);
                }
                objMap.put(objName,fbL);

            }
            
        

        return objMap;
    }

    //
     public static  SObjectType getSObjectType(String sobjectName ){
          ///获取所有对象
        for(Schema.sobjecttype s: Schema.getGlobalDescribe().values()){
            if(s.getDescribe().getName().toLowerCase() == sobjectName.toLowerCase()) return s ;                                        
        }
        return null;
    }
    //获取字段 的信息
    public static  Map<String,FieldBean>  getFieldsInfo(SObjectType stype , Set<String> fields ) {
        Map<String,FieldBean> fieldMap = new Map<String,FieldBean>();
        for(String k : fields){
                   
            FieldBean fb = null ;

            if(k.contains('.')){
                List<String>keys =  k.split('\\.');

                SObjectType initType= stype;
                Integer i = 0 ; 
                String label = null;
                for( String fk : keys){
                    if(fk.contains('__r'))fk = fk.replace('__r', '__c');
                    System.debug(fk);
                    FieldBean tmpfb = new FieldBean(initType.getDescribe().fields.getMap().get(fk).getDescribe(),initType.getDescribe().getName());
                    if(i == 0 )fb = tmpfb ;
                    i++;
                    if(i == keys.size() ){
                        fb.name = k ;
                        fb.data = tmpfb.data;
                        //fb.reference  = null ; 
                    }
                    if(fb.reference != null )initType =getSObjectType(fb.reference.objname); 
                   System.debug('DataUtil==>'+fb);
                }

            }else{
                System.debug('DataUtil==>'+k);
                 fb = new FieldBean(stype.getDescribe().fields.getMap().get(k).getDescribe(),stype.getDescribe().getName());
            }
            //FieldBean fb = new FieldBean(stype.getDescribe().fields.getMap().get(k).getDescribe(),stype.getDescribe().getName());

            fieldMap.put(fb.name,fb);
        }
        return fieldMap;

    }

    public static Map<String,FieldBean> getDetailBean( Map<String,String> fieldMap ,SObjectType stype ){

       Map<String,FieldBean> fieldBMap =  getFieldsInfo(stype,fieldMap.keySet());

       for(String fieldname : fieldMap.keySet()){
            FieldBean fb = fieldBMap.get(fieldname) ;
            fb.name = fieldMap.get(fieldname);
            fb.alias = fieldname;
       }
       return fieldBMap;
    }


    public static  Map<String ,Object>  getBaseObjectMap (Map<String,SObjectBean> sm ){

        Map<String ,Object >  sob = new Map<String ,Object>();
        for(String objname : sm.keySet()){
            if(sm.get(objname).type == 'base'){
                 Map<String ,String >  sobm = new Map<String ,String>();
                sobm.put('title', sm.get(objname).title);
                sob.put(objname, sobm);
            }
          
        }

        return sob ;

    }
     public static  Map<String ,Object>  getDetailObjectMap (Map<String,SObjectBean> sm ){

        Map<String ,Object >  sob = new Map<String ,Object>();
        
        for(String objname : sm.keySet()){
            if(sm.get(objname).type == 'detail'){
                 Map<String ,String >  sobm = new Map<String ,String>();
                sobm.put('title', sm.get(objname).title);
                sob.put(objname, sobm);
            }
        }
        return sob ;

    }
    public class SObjectBean{
        public String title ;
        public String objname; 
        public SObjectType stype;
        public String idvalue;
        public String type ;
        public String condition ; 

    }
    public class FieldBean{

        public String name;
        public String alias;
        public String label;
        public Object value;
        public String type;
        public String objname; 
        public List<Map<String,String>> data;///选项列表
        public FieldBean reference ;
        public String relation ;
        public Boolean isRecordtype ;
        public List<String> parents ;

        public FieldBean(){}
        public FieldBean(Schema.DescribeFieldResult result,String sobjectName  ){
            name = result.getName();
            label = result.getLabel();
            type = (result.getType()+'').toLowerCase();
            data = new List<Map<String,String>> ();
            objname = sobjectName ; 
            relation = result.getRelationshipName();

            if(type == 'reference'){
                SObjectType st = result.getReferenceTo().get(0);
                FieldBean fb = new FieldBean();
                fb.name = 'Name';
                fb.label = st.getDescribe().getLabel();
                fb.objname = st.getDescribe().getName();
                isRecordtype = Schema.getGlobalDescribe().get(fb.objname).getDescribe().getRecordTypeInfosByName().size() > 1; 
                fb.type = 'string';
                fb.isRecordtype = isRecordtype;
                reference = fb ;
                if(result.getFilteredLookupInfo() != null)parents = result.getFilteredLookupInfo().getControllingFields();
            }
            if(type.contains('picklist')){
                Map<String,String> pickMap = ImportObject.getPicklistValues(sobjectName,name, true);
                for(String k :pickMap.keySet()){
                    Map<String,String> picklist = new Map<String,String>();
                    picklist.put('id', k);
                    picklist.put('text', pickMap.get(k));
                    data.add(picklist);
                }
            } 
        }

    }
}